﻿using Syncfusion.Licensing.crypto;
using Syncfusion.Licensing.crypto.encodings;
using Syncfusion.Licensing.crypto.engines;
using Syncfusion.Licensing.crypto.parameters;
using Syncfusion.Licensing.math;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 天机万年历
{
    public class SyncfusionCrack
    {
        public static string ByteArray2String(byte[] bs)
        {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bs.Length; i++)
            {
                stringBuilder.Append(Convert.ToChar(bs[i]));
            }
            return stringBuilder.ToString();
        }
        public static byte[] String2ByteArray(string str)
        {
            List<byte> rs = new List<byte>();
            for (int i = 0; i < str.Length; i++)
            {
                rs.Add(Convert.ToByte(str[i]));
            }
            return rs.ToArray();
        }
        public static byte[] HexDecode(string data)
        {
            byte[] array = new byte[data.Length / 2];
            string text = data.ToLower();
            for (int i = 0; i < text.Length; i += 2)
            {
                char c = text[i];
                char c2 = text[i + 1];
                int num = i / 2;
                if (c < 'a')
                {
                    array[num] = (byte)(c - '0' << 4);
                }
                else
                {
                    array[num] = (byte)(c - 'a' + '\n' << 4);
                }
                if (c2 < 'a')
                {
                    byte[] array2 = array;
                    int num2 = num;
                    array2[num2] += (byte)(c2 - '0');
                }
                else
                {
                    byte[] array3 = array;
                    int num3 = num;
                    array3[num3] += (byte)(c2 - 'a' + '\n');
                }
            }
            return array;
        }
        public static string HexEncode(byte[] bytes)
        {
            StringBuilder sb = new StringBuilder();
            if (bytes != null || bytes.Length > 0)
            {
                foreach (var item in bytes)
                {
                    sb.Append(item.ToString("x2"));
                }
            }
            return sb.ToString();
        }
        public class PrivateKeyEncrypt
        {
            public static string SyncfusionEncode(byte[] buffer)
            {
                return PrivateKeyEncrypt.EncodeLongBuffer(buffer, PrivateKeyEncrypt.priParameters);
            }

            private static string EncodeLongBuffer(byte[] buffer, RSAKeyParameters priParameters)
            {
                var b = PrivateKeyEncrypt.EncodeLongBuffer_(buffer, priParameters);
                return Convert.ToBase64String(b);
            }

            private static byte[] EncodeLongBuffer_(byte[] buffer, RSAKeyParameters priParameters)
            {
                AsymmetricBlockCipher cipher = new RSAEngine();
                PKCS1Encoding pkcs1Encoding = new PKCS1Encoding(cipher);
                pkcs1Encoding.init(true, priParameters);
                byte[] result;
                try
                {
                    ArrayList arrayList = new ArrayList();
                    int inputBlockSize = pkcs1Encoding.getInputBlockSize();
                    result = pkcs1Encoding.processBlock(buffer, 0, buffer.Length);
                    //int num = buffer.Length / inputBlockSize;
                    //new ArrayList();
                    //for (int i = 0; i < num; i++)
                    //{
                    //    int num2 = i * inputBlockSize;
                    //    int inLen = Math.Min(buffer.Length - num2, inputBlockSize);
                    //    byte[] array = pkcs1Encoding.processBlock(buffer, num2, inLen);
                    //    //byte[] array2 = new byte[(int)array[0]];
                    //    //Array.Copy(array, 1, array2, 0, array2.Length);
                    //    arrayList.AddRange(array);
                    //}
                    //byte[] array3 = new byte[arrayList.Count];
                    //arrayList.CopyTo(array3);
                    //result = array3;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("RSA: failed - exception " + ex.ToString());
                    result = new byte[0];
                }
                return result;
            }

            internal static RSAKeyParameters priParameters = new RSAKeyParameters(true, new BigInteger("74311111206657030479635290206242922294134948120327310989730425327779900016841"), new BigInteger("17"));
        }
    }
}
